package com.me.components.pop {
	import com.me.components.MapResource;
	import com.me.components.fix.MapResourceLayer;
	import com.me.components.map.MapScene;
	import com.me.obj.Building;

	import flash.events.MouseEvent;

	import mx.core.FlexGlobals;
	import mx.events.CloseEvent;

	import spark.components.Button;
	import spark.components.Group;
	import spark.components.RadioButton;
	import spark.components.RadioButtonGroup;
	import spark.components.TitleWindow;
	import spark.core.SpriteVisualElement;

	/**
	 *
	 * @author yw
	 *
	 * @Date 2011-9-19 上午09:52:16
	 */
	public class MapLayerOption extends TitleWindow {
		private static var _instance:MapLayerOption;

		private var _radioGroup:RadioButtonGroup;

		private var _confirmButton:Button;

		private var _cancelButton:Button;

		private var _res:MapResource;

		public static function getInstance():MapLayerOption {
			if (_instance == null) {
				_instance = new MapLayerOption(new SingletonEnforce());
			}
			return _instance;
		}

		public function MapLayerOption(enforce:SingletonEnforce) {
			if (enforce == null) {
				throw new Error("MapLayerOption can not be intance");
			}
			layoutComponent();
		}

		private function layoutComponent():void {
			title = "资源添加到";
			width = 275;
			height = 150;

			_radioGroup = new RadioButtonGroup();

			var radio:RadioButton = new RadioButton();
			radio.label = "地表层";
			radio.x = 32;
			radio.y = 24;
			radio.name = "surface";
			radio.group = _radioGroup;
			addElement(radio);

			radio = new RadioButton();
			radio.label = "建筑层";
			radio.x = 32;
			radio.y = 46;
			radio.name = "building";
			radio.selected = true;
			radio.group = _radioGroup;
			addElement(radio);

			_confirmButton = new Button();
			_confirmButton.label = "确定";
			_confirmButton.y = 80;
			_confirmButton.x = 54;
			addElement(_confirmButton);

			_cancelButton = new Button();
			_cancelButton.label = "取消";
			_cancelButton.y = 80;
			_cancelButton.x = 162;
			addElement(_cancelButton);

			addEventListener(CloseEvent.CLOSE, closeHandler);
			_confirmButton.addEventListener(MouseEvent.CLICK, buttonClickHandler);
			_cancelButton.addEventListener(MouseEvent.CLICK, buttonClickHandler);
		}

		private function buttonClickHandler(e:MouseEvent):void {
			if (_confirmButton == e.currentTarget) {
				if (_radioGroup.selection.name == "building") {
					MapScene.getInstance().addBuilding(_res);
				} else {
					MapScene.getInstance().addSurface(_res);
				}
				_res = null;
			}
			dispatchEvent(new CloseEvent(CloseEvent.CLOSE));
		}

		private function closeHandler(e:CloseEvent):void {
			(parent as Group).removeElement(this);
		}

		public function recoordinate():void {
			this.x = (parent.width - width) / 2;
			this.y = (parent.height - height) / 2;
		}

		public function set res(value:MapResource):void {
			_res = value;
		}

	}
}

class SingletonEnforce {

}